package com.aos.eap.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.base.BaseDialogActivity;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.ExchangeClassBean;
import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.MyUtils;
import com.aos.eap.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;

public class MFG_ExchangeClassActivity extends BaseDialogActivity {


    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.tvEqpStatus)
    TextView tvEqpStatus;
    @BindView(R.id.tvLotId)
    TextView tvLotId;
    @BindView(R.id.tvLotNum)
    TextView tvLotNum;
    @BindView(R.id.etAlreadyDoneNum)
    FreeUI_ClearEditText etAlreadyDoneNum;
    @BindView(R.id.tvLostNum)
    TextView tvLostNum;
    @BindView(R.id.etRemark)
    FreeUI_ClearEditText etRemark;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private ExchangeClassBean exchangeClassBean;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_exchange_class;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);
        MyUtils.setEditTextListener(etEqpId, this, this::requestInfoByEqpId);
        etAlreadyDoneNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (exchangeClassBean != null) {
                    if (s.length() > 0) {

                        float num = Float.parseFloat(s.toString());
                        float lostNum = exchangeClassBean.getLotQTY() - num;
                        tvLostNum.setText(String.valueOf(lostNum));
                    } else {
                        etAlreadyDoneNum.setText("0");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void requestInfoByEqpId(String eqpId) {
        JsonObject map = new JsonObject();
        map.addProperty("EqpID", eqpId);
        NetUtils.requestGetNet(this, "EMS_ShiftLoad", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<ExchangeClassBean>>(
                new TypeToken<BaseResponseBean<ExchangeClassBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<ExchangeClassBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true,getSupportFragmentManager())) {
                    exchangeClassBean = response.body().getResponse().getBody();
                    setPageData(exchangeClassBean);
                }else {
                    exchangeClassBean = null;
                    setPageData(new ExchangeClassBean());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<ExchangeClassBean>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_ShiftLoad");
                exchangeClassBean = null;
                setPageData(new ExchangeClassBean());
            }
        });
    }
    private void setPageData(ExchangeClassBean exchangeClassBean) {
        tvEqpStatus.setText(exchangeClassBean.getMesMode());
        tvLotId.setText(exchangeClassBean.getLotID());
        tvLotNum.setText(exchangeClassBean.getLotQTY()+"");
        etAlreadyDoneNum.setText(exchangeClassBean.getCompleteQTY()+"");
        float num = exchangeClassBean.getLotQTY() - exchangeClassBean.getCompleteQTY();
        tvLostNum.setText(String.valueOf(num));
    }
    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("交接班");
        freeUI_titleView.setLeftListener(view -> showBackDialog(MFG_ExchangeClassActivity.this));
        freeUI_titleView.setRightListener("提交", v -> {
            if (MyUtils.isFastClick()){
                showToast("您点击的太快了",false);
                return;
            }
            if (exchangeClassBean == null) {
                showToast("未获取到页面信息，请重新扫描设备编号", false);
                MyUtils.setEditTextFocus(etEqpId);
                etEqpId.setText("");
                return;
            }
            if (Float.parseFloat(tvLostNum.getText().toString()) < 0) {
                showToast("剩余数量不能小于0", false);
                MyUtils.setEditTextFocus(etAlreadyDoneNum);
                return;
            }
            JsonObject map = new JsonObject();
            map.addProperty("EqpID", exchangeClassBean.getEqpID());
            map.addProperty("LotID", exchangeClassBean.getLotID());
            map.addProperty("OPID", Constant.LAST_USER);
            map.addProperty("ToOPID", Constant.CURRENT_USER);
            map.addProperty("LotQTY", exchangeClassBean.getLotQTY());
            map.addProperty("CompleteQTY", Float.parseFloat(etAlreadyDoneNum.getText().toString()));
            map.addProperty("Remark", etRemark.getText().toString());
            requestExchangeClass(map);
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    private void requestExchangeClass(JsonObject map) {
        NetUtils.requestGetNet(this, "EMS_Shift", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                new TypeToken<BaseResponseBean<?>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
                if (response.body().isSuccess(getApplicationContext(), false,getSupportFragmentManager())) {
                    showToast("提交成功", true);
                    clearAll();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_Shift");
            }
        });
    }

    private void clearAll() {
        etEqpId.setText("");
        tvEqpStatus.setText("");
        tvLotId.setText("");
        tvLotNum.setText("");
        etAlreadyDoneNum.setText("");
        tvLostNum.setText("");
        etRemark.setText("");
    }

}
